<template>
  <div class="mainbody">
    <bpm-c-subnavbar @navClicked="navClicked"
                     :showLeft="false"
                     :rightShowArray="['save', 'close']"/>
    <div>
      <bpm-c-form>
      <bpm-c-block :ptitle="getLanguage('label.hsInputGBO.creator.userName')"
                   :preadonly="true"
                   pinputtype="text"
                   v-model="input1" />
      <bpm-c-block :ptitle="getLanguage('label.bpdName')"
                   :preadonly="true"
                   pinputtype="text"
                   v-model="input2" />
      </bpm-c-form>
    </div>
    <bpm-d-grid
      :needBar="false"
      :height="458"
      :showCheckbox="false"
      :gridInfo="gridInfo"
      :gridTitleInfo="gridTitleInfo"
      :gridShowInfo="gridShowInfo"
      :gridTypeInfo="gridTypeInfo"
      :gridWidthInfo="gridWidthInfo"
      :pageMax="pageMax"
      :pageStart="pageStart"
      :pageCount="pageCount"
      @gridStatusChanged="gridStatusChanged"
      @gridPageChanged="gridPageChanged"
      @gridClicked="gridClicked"
      @gridDBLClicked="selectedClicked"/>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'
export default {
  name: 'bpmDTaskchoose',
  props: {
    title: {
      type: String,
      default: ''
    },
    pselectType: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      encryptProcessLink: '',
      gridShowInfo: ['bBpdNameLocale', 'piid', 'createTime', 'btNo', 'completeTime', 'deptName'],
      gridWidthInfo: [100, 80, 140, 140, 140, 380],
      gridTypeInfo: ['text', 'text', 'text', 'text', 'text', 'text'],
      chooseIndex: -1
    }
  },
  methods: {
    ...mapActions(['queryAppListByType', 'encryptTsIdForHyperView']),
    navClicked (atype) {
      console.log('task type:', atype)
      if (atype === 'close') {
        this.$emit('closeClicked')
      } else if (atype === 'save') {
        console.log(this.chooseIndex)
        if (this.chooseIndex >= 0) {
          console.log('selectedClicked', this.pselectType)
          if (this.pselectType === this.chooseDialogEnum.trainlink || this.pselectType === this.chooseDialogEnum.aboardlink) {
            this.encryptProcessLink = ''
            // 调用服务加密tsPiid,更新链接至主表数据中心
            this.encryptTsIdForHyperView({me: this, tsId: this.gridInfo[this.chooseIndex].id})
            console.log('*******window.location.protocol*******', window.location.protocol)
            console.log('*******window.location.host*******', window.location.host)
            this.encryptProcessLink = window.location.protocol + '//' + window.location.host + '/ECS_BPM_ADV/process/hyperView.action?tspiid=' + this.encryptTsId
            console.log('encryptProcessLink', this.encryptProcessLink)
            if (this.pselectType === this.chooseDialogEnum.trainlink) {
              this.$store.commit('updateDataSscReimburseWhBtReimburse', {
                name: 'trainapplicationlink',
                value: this.encryptProcessLink
              })
            } else if (this.pselectType === this.chooseDialogEnum.aboardlink) {
              this.$store.commit('updateDataSscReimburseWhBtReimburse', {
                name: 'abroadapplicationlink',
                value: this.encryptProcessLink
              })
            }
            console.log('updateDataSscReimburseWhBtReimburse process link', this.encryptProcessLink)
          }
          this.$emit('selectClicked', this.gridInfo[this.chooseIndex])
        } else {
          // this.tips('请选择一行记录进行保存操作')
        }
      }
    },
    gridClicked (aindex) {
      this.chooseIndex = aindex
    },
    selectedClicked (aindex) {
      console.log('selectedClicked', this.pselectType)
      if (this.pselectType === this.chooseDialogEnum.trainlink || this.pselectType === this.chooseDialogEnum.aboardlink) {
        this.encryptProcessLink = ''
        // 调用服务加密tsPiid,更新链接至主表数据中心
        this.encryptTsIdForHyperView({me: this, tsId: this.gridInfo[aindex].id})
        console.log('*******window.location.protocol*******', window.location.protocol)
        console.log('*******window.location.host*******', window.location.host)
        this.encryptProcessLink = window.location.protocol + '//' + window.location.host + '/ECS_BPM_ADV/process/hyperView.action?tspiid=' + this.encryptTsId
        console.log('encryptProcessLink', this.encryptProcessLink)
        if (this.pselectType === this.chooseDialogEnum.trainlink) {
          this.$store.commit('updateDataSscReimburseWhBtReimburse', {
            name: 'trainapplicationlink',
            value: this.encryptProcessLink
          })
        } else if (this.pselectType === this.chooseDialogEnum.aboardlink) {
          this.$store.commit('updateDataSscReimburseWhBtReimburse', {
            name: 'abroadapplicationlink',
            value: this.encryptProcessLink
          })
        }
        console.log('updateDataSscReimburseWhBtReimburse process link', this.encryptProcessLink)
      }
      console.log('choose', aindex)
      this.$emit('selectClicked', this.gridInfo[aindex])
    },
    gridStatusChanged (v) {
      // do nothing
    },
    gridPageChanged (v) {
      console.log('payment', v)
      if (v.name === 'count' || v.name === 'start') {
        let path = ''
        if (this.pselectType === this.chooseDialogEnum.trainlink) {
          path = 'trainApplyList'
        } else if (this.pselectType === this.chooseDialogEnum.aboardlink) {
          path = 'abroadApplyList'
        }
        let json = {
          path: path,
          name: v.name,
          value: v.value
        }
        this.$store.commit('upGridPageInfoWithPathAndValue', json)
        let bpdname = 'SSC.Non-ContractPay'
        if (this.pselectType === this.chooseDialogEnum.trainlink) {
          bpdname = 'HR.StaffTrain'
        } else if (this.pselectType === this.chooseDialogEnum.aboardlink) {
          bpdname = 'HRV3.FrgnaMgmt-VisaApply'
        }
        this.queryAppListByType({me: this, bpdName: bpdname})
      }
    }
  },
  computed: {
    ...mapState(['ssc', 'chooseDialogEnum', 'gridPageInfo', 'lang', 'encryptTsId']),
    blockTitleInfo () {
      return [this.getLanguage('label.hsInputGBO.creator.userName'),
        this.getLanguage('label.bpdName')]
    },
    gridTitleInfo () {
      return [
        this.getLanguage('label.tsvApplication.bBpdNameLocale'),
        this.getLanguage('label.tsvApplication.piid'),
        this.getLanguage('label.tsvApplication.createTime'),
        this.getLanguage('label.tsvApplication.btNo'),
        this.getLanguage('label.tsvApplication.completeTime'),
        this.getLanguage('label.tsvApplication.deptName')
      ]
    },
    pageMax () {
      if (this.pselectType === this.chooseDialogEnum.trainlink) {
        return this.gridPageInfo.ssc.reimburse.trainApplyList.max
      } else if (this.pselectType === this.chooseDialogEnum.aboardlink) {
        return this.gridPageInfo.ssc.reimburse.abroadApplyList.max
      }
    },
    pageStart () {
      if (this.pselectType === this.chooseDialogEnum.trainlink) {
        return this.gridPageInfo.ssc.reimburse.trainApplyList.start
      } else if (this.pselectType === this.chooseDialogEnum.aboardlink) {
        return this.gridPageInfo.ssc.reimburse.abroadApplyList.start
      }
    },
    pageCount () {
      if (this.pselectType === this.chooseDialogEnum.trainlink) {
        return this.gridPageInfo.ssc.reimburse.trainApplyList.count
      } else if (this.pselectType === this.chooseDialogEnum.aboardlink) {
        return this.gridPageInfo.ssc.reimburse.abroadApplyList.count
      }
    },
    input1 () {
      return this.$store.state.userId
    },
    input2 () {
      if (this.pselectType === this.chooseDialogEnum.trainlink) {
        return this.ssc.reimburse.trainApplyTitle
      } else if (this.pselectType === this.chooseDialogEnum.aboardlink) {
        return this.ssc.reimburse.abroadApplyTitle
      }
    },
    gridInfo () {
      if (this.pselectType === this.chooseDialogEnum.trainlink) {
        return this.ssc.reimburse.trainApplyList
      } else if (this.pselectType === this.chooseDialogEnum.aboardlink) {
        return this.ssc.reimburse.abroadApplyList
      }
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .mainbody {
    width: 980px;
    min-height: 300px;
  }
</style>
